package com.dirk.happie.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dirk.happie.beans.ArticleBean;
import com.dirk.happie.beans.BasketItem;
import com.dirk.happie.beans.CompanyBean;
import com.dirk.happie.beans.SettingsBean;

public class Common {

	public static String getMD5(String s) {
		MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("MD5");
			digest.update(s.getBytes(), 0, s.length());
			String hash = new BigInteger(1, digest.digest()).toString(16);
			return hash;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static void printStream(InputStream is) {
		BufferedReader r = new BufferedReader(new InputStreamReader(is));
		StringBuilder total = new StringBuilder();
		String line;
		try {
			while ((line = r.readLine()) != null) {
				total.append(line);
			}
			System.out.println("total = " + total);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getPrice(String price) {
		price = price.replaceAll(",", ".");
		float myPrice = Float.parseFloat(price);
		String retValue = String.format(Locale.US, "%.2f", myPrice);
		return retValue;
	}

	public static boolean addFavoriteIfExists(ArrayList<CompanyBean> favorites, CompanyBean company) {
		boolean notExists = true;
		for (CompanyBean companyBean : favorites) {
			if (companyBean.getCompanyId().equals(company.getCompanyId())) {
				notExists = false;
			}
		}
		if (notExists) {
			favorites.add(company);
		}
		return notExists;
	}

	public static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	public static boolean compareOfferDate(ArticleBean curArticle) {
		DateFormat outputDF;
		Date nowDate = null;
		outputDF = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
		Calendar c = Calendar.getInstance();

		Date startDate = null;
		Date endDate = null;
		try {
			nowDate = outputDF.parse(Common.pad(c.get(Calendar.YEAR)) + "-" + Common.pad(c.get(Calendar.MONTH) + 1) + "-" + Common.pad(c.get(Calendar.DAY_OF_MONTH)));
			if (curArticle.getOfferExist() != null && curArticle.getOfferExist().equals("Y") && curArticle.getOfferStartDate() != null && curArticle.getOfferEndDate() != null && curArticle.getOfferPrice() != null && !curArticle.getOfferPrice().equals("")) {
				startDate = outputDF.parse(curArticle.getOfferStartDate());
				endDate = outputDF.parse(curArticle.getOfferEndDate());
				if (nowDate.compareTo(startDate) >= 0 && nowDate.compareTo(endDate) <= 0) {
					return true;
				}
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public static String companyArrayToJson(List<CompanyBean> result) {

		JSONArray array = new JSONArray();
		try {
			for (CompanyBean companyBean : result) {
				JSONObject json = new JSONObject();

				json.putOpt("idcompany", companyBean.getCompanyId());
				json.putOpt("passwd", companyBean.getCompanyPassword());
				json.putOpt("company_name", companyBean.getCompanyName());
				json.putOpt("address", companyBean.getCompanyAddress());
				json.putOpt("zip", companyBean.getCompanyZip());
				json.putOpt("city", companyBean.getCompanyCity());
				json.putOpt("country", companyBean.getCompanyCountry());
				json.putOpt("phone", companyBean.getCompanyPhone());
				json.putOpt("email", companyBean.getCompanyEmail());
				json.putOpt("logo", companyBean.getCompanyLogo());
				json.putOpt("website", companyBean.getCompanyWebsite());
				json.putOpt("pos_x", companyBean.getCompanyPosX());
				json.putOpt("pos_y", companyBean.getCompanyPosY());
				json.putOpt("contact_name", companyBean.getCompanyId());
				json.putOpt("contact_surname", companyBean.getCompanyId());
				json.putOpt("contact_gender", companyBean.getCompanyId());

				array.put(json);

			}
		} catch (JSONException e) {

		}

		return array.toString();
	}

	public static ArrayList<CompanyBean> jsonToCompanyArray(String jsonString) {
		ArrayList<CompanyBean> companies = new ArrayList<CompanyBean>();

		try {
			JSONArray array = new JSONArray(jsonString);

			for (int i = 0; i < array.length(); i++) {

				CompanyBean companyBean = new CompanyBean();
				JSONObject json = array.getJSONObject(i);

				companyBean.setCompanyId(json.optString("idcompany"));
				companyBean.setCompanyPassword(json.optString("passwd"));
				companyBean.setCompanyName(json.optString("company_name"));
				companyBean.setCompanyAddress(json.optString("address"));
				companyBean.setCompanyZip(json.optString("zip"));
				companyBean.setCompanyCity(json.optString("city"));
				companyBean.setCompanyCountry(json.optString("country"));
				companyBean.setCompanyPhone(json.optString("phone"));
				companyBean.setCompanyEmail(json.optString("email"));
				companyBean.setCompanyLogo(json.optString("logo"));
				companyBean.setCompanyWebsite(json.optString("website"));
				companyBean.setCompanyPosX(json.optString("pos_x"));
				companyBean.setCompanyPosY(json.optString("pos_y"));
				companyBean.setCompanyContactName(json.optString("contact_name"));
				companyBean.setCompanyContactSurname(json.optString("contact_surname"));
				companyBean.setCompanyContactGender(json.optString("contact_gender"));

				companies.add(companyBean);

			}
		} catch (JSONException e) {

		}

		return companies;
	}

	public static String settingsBeanToJson(SettingsBean settingsBean) {

		JSONObject json = new JSONObject();
		try {
			json.putOpt("min_oder_value", settingsBean.getMinOrderValue());
			json.putOpt("delivery_cost", settingsBean.getDeliveryCost());
			json.putOpt("mon_op", settingsBean.getMonOpen());
			json.putOpt("tue_op", settingsBean.getTueOpen());
			json.putOpt("wed_op", settingsBean.getWedOpen());
			json.putOpt("thu_op", settingsBean.getThuOpen());
			json.putOpt("fri_op", settingsBean.getFriOpen());
			json.putOpt("sat_op", settingsBean.getSatOpen());
			json.putOpt("sun_op", settingsBean.getSunOpen());
			json.putOpt("dmon_from", settingsBean.getDmonFrom());
			json.putOpt("dmon_to", settingsBean.getDmonTo());
			json.putOpt("dtue_from", settingsBean.getDtueFrom());
			json.putOpt("dtue_to", settingsBean.getDtueTo());
			json.putOpt("dwed_from", settingsBean.getDwedFrom());
			json.putOpt("dwed_to", settingsBean.getDwedTo());
			json.putOpt("dthu_from", settingsBean.getDthuFrom());
			json.putOpt("dthu_to", settingsBean.getDthuTo());
			json.putOpt("dfri_from", settingsBean.getDfriFrom());
			json.putOpt("dfri_to", settingsBean.getDfriTo());
			json.putOpt("dsat_from", settingsBean.getDsatFrom());
			json.putOpt("dsat_to", settingsBean.getDsatTo());
			json.putOpt("dsun_from", settingsBean.getDsunFrom());
			json.putOpt("dsun_to", settingsBean.getDsunTo());
			json.putOpt("pmon_from", settingsBean.getPmonFrom());
			json.putOpt("pmon_to", settingsBean.getPmonTo());
			json.putOpt("ptue_from", settingsBean.getPtueFrom());
			json.putOpt("ptue_to", settingsBean.getPtueTo());
			json.putOpt("pwed_from", settingsBean.getPwedFrom());
			json.putOpt("pwed_to", settingsBean.getPwedTo());
			json.putOpt("pthu_from", settingsBean.getPthuFrom());
			json.putOpt("pthu_to", settingsBean.getPthuTo());
			json.putOpt("pfri_from", settingsBean.getPfriFrom());
			json.putOpt("pfri_to", settingsBean.getPfriTo());
			json.putOpt("psat_from", settingsBean.getPsatFrom());
			json.putOpt("psat_to", settingsBean.getPsatTo());
			json.putOpt("psun_from", settingsBean.getPsunFrom());
			json.putOpt("psun_to", settingsBean.getPsunTo());
			json.putOpt("delivery_status", settingsBean.getDeliveryStatus());
			json.putOpt("pickup_status", settingsBean.getPickupStatus());
			json.putOpt("closed_day_1", settingsBean.getClosedDay1());
			json.putOpt("closed_day_2", settingsBean.getClosedDay2());
			json.putOpt("closed_day_3", settingsBean.getClosedDay3());
			json.putOpt("closed_day_4", settingsBean.getClosedDay4());
		} catch (JSONException e) {

		}

		return json.toString();
	}

	public static SettingsBean jsonToSettingsBean(String jsonString) {
		SettingsBean settingsBean = new SettingsBean();
		try {
			JSONObject json = new JSONObject(jsonString);

			settingsBean.setMinOrderValue(json.optString("min_oder_value"));
			settingsBean.setDeliveryCost(json.optString("delivery_cost"));
			settingsBean.setMonOpen(json.optString("mon_op"));
			settingsBean.setTueOpen(json.optString("tue_op"));
			settingsBean.setWedOpen(json.optString("wed_op"));
			settingsBean.setThuOpen(json.optString("thu_op"));
			settingsBean.setFriOpen(json.optString("fri_op"));
			settingsBean.setSatOpen(json.optString("sat_op"));
			settingsBean.setSunOpen(json.optString("sun_op"));
			settingsBean.setDmonFrom(json.optString("dmon_from"));
			settingsBean.setDmonTo(json.optString("dmon_to"));
			settingsBean.setDtueFrom(json.optString("dtue_from"));
			settingsBean.setDtueTo(json.optString("dtue_to"));
			settingsBean.setDwedFrom(json.optString("dwed_from"));
			settingsBean.setDwedTo(json.optString("dwed_to"));
			settingsBean.setDthuFrom(json.optString("dthu_from"));
			settingsBean.setDthuTo(json.optString("dthu_to"));
			settingsBean.setDfriFrom(json.optString("dfri_from"));
			settingsBean.setDfriTo(json.optString("dfri_to"));
			settingsBean.setDsatFrom(json.optString("dsat_from"));
			settingsBean.setDsatTo(json.optString("dsat_to"));
			settingsBean.setDsunFrom(json.optString("dsun_from"));
			settingsBean.setDsunTo(json.optString("dsun_to"));
			settingsBean.setPmonFrom(json.optString("pmon_from"));
			settingsBean.setPmonTo(json.optString("pmon_to"));
			settingsBean.setPtueFrom(json.optString("ptue_from"));
			settingsBean.setPtueTo(json.optString("ptue_to"));
			settingsBean.setPwedFrom(json.optString("pwed_from"));
			settingsBean.setPwedTo(json.optString("pwed_to"));
			settingsBean.setPthuFrom(json.optString("pthu_from"));
			settingsBean.setPthuTo(json.optString("pthu_to"));
			settingsBean.setPfriFrom(json.optString("pfri_from"));
			settingsBean.setPfriTo(json.optString("pfri_to"));
			settingsBean.setPsatFrom(json.optString("psat_from"));
			settingsBean.setPsatTo(json.optString("psat_to"));
			settingsBean.setPsunFrom(json.optString("psun_from"));
			settingsBean.setPsunTo(json.optString("psun_to"));
			settingsBean.setDeliveryStatus(json.optString("delivery_status"));
			settingsBean.setPickupStatus(json.optString("pickup_status"));
			settingsBean.setClosedDay1(json.optString("closed_day_1"));
			settingsBean.setClosedDay2(json.optString("closed_day_2"));
			settingsBean.setClosedDay3(json.optString("closed_day_3"));
			settingsBean.setClosedDay4(json.optString("closed_day_4"));

		} catch (JSONException e) {
		}

		return settingsBean;
	}

	public static String basketArrayToJson(ArrayList<BasketItem> result) {

		JSONArray array = new JSONArray();
		try {
			for (BasketItem basketItem : result) {
				JSONObject json = new JSONObject();

				json.putOpt("count", basketItem.getCount());
				json.putOpt("name", basketItem.getName());
				json.putOpt("size", basketItem.getSize());
				json.putOpt("price", basketItem.getPrice());
				json.putOpt("idArticle", basketItem.getIdArticle());
				json.putOpt("idOption", basketItem.getIdOption());
				json.putOpt("idExtra", basketItem.getIdExtra());
				json.putOpt("type", basketItem.getType());
				array.put(json);

			}
		} catch (JSONException e) {

		}

		return array.toString();
	}

	public static ArrayList<BasketItem> jsonToBasketArray(String jsonString) {
		ArrayList<BasketItem> basketList = new ArrayList<BasketItem>();

		try {
			JSONArray array = new JSONArray(jsonString);

			for (int i = 0; i < array.length(); i++) {

				BasketItem basketBean = new BasketItem();
				JSONObject json = array.getJSONObject(i);

				basketBean.setCount(json.optString("count"));
				basketBean.setName(json.optString("name"));
				basketBean.setSize(json.optString("size"));
				basketBean.setPrice(json.optString("price"));
				basketBean.setIdArticle(json.optString("idArticle"));
				basketBean.setIdOption(json.optString("idOption"));
				basketBean.setIdExtra(json.optString("idExtra"));
				basketBean.setType(json.optString("type"));

				basketList.add(basketBean);

			}
		} catch (JSONException e) {

		}

		return basketList;
	}
}
